// use serde_with::serde_as;
use serde::{Deserialize, Serialize};

use crate::serde::{string_other, other_string};

/// 分页参数
// #[serde_as]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, validator::Validate)]
pub struct PageParam {
    /// 当前页码
    #[validate(range(min = 1, message = "页码必须大于0"))]
    #[serde(default = "default_current", deserialize_with = "string_other")]
    // #[serde_as(as = "OnlyParse")]
    pub current: u32,
    /// 每页条数
    #[validate(range(min = 1, max = 100, message = "分页大小必须在1到100之间"))]
    #[serde(default = "default_size", deserialize_with = "string_other")]
    // #[serde_as(as = "OnlyParse")]
    pub size: u32
}

fn default_current() -> u32 {
    1
}

fn default_size() -> u32 {
    10
}

/// 分页结果
// #[serde_as]
#[derive(Debug, Serialize)]
pub struct Page<T> {
    pub current: u32,
    pub size: u32,
    #[serde(serialize_with = "other_string")]
    // #[serde_as(as = "DisplayParse")]
    pub total: u64,
    pub records: Vec<T>
}

impl<T> Page<T> {
    pub fn new(current: u32, size: u32, total: u64, records: Vec<T>) -> Self {
        Self { current, size, total, records }
    }

    pub fn from_pagination(pagination: PageParam, total: u64, records: Vec<T>) -> Self {
        Self::new(pagination.current, pagination.size, total, records)
    }
}